<?php
/**
 * 会员卡关联表模型
 * @author tangbl
 *
 */
class VipUserModel extends Model {
    
	/**
	 array(
 		0.字段名
		1.验证规则 [require 字段必须、email 邮箱、url URL地址、currency 货币、number 数字]
		2.提示信息  {%usr_form_validata_email_email}
		3.验证条件 [0.(默认)存在字段就验证;1.必须验证;2.值不为空的时候验证]
		4.附加规则 [regex、function、callback、confirm、equal、in、length、between、
		         expire、ip_allow、ip_deny、unique]
		5.验证时   [1.新增数据时候验证;2.编辑数据时候验证;3.(默认)全部情况下验证]
	  );
	 */
	protected $_validate = array(

	);
	
	/**
	 * 定义自动完成
	 */
	protected $_auto     = array(
	);
	
	/**
	 * 数据库表名
	 */
	protected $tableName = 'vipcard_user';
	
	
	/**
	 * 新增实体会员卡
	 * @access public
	 * @param  Int    cardid   会员卡主键id
     * @param  Int    customid 会员信息id
     * @param  Int    company  商户id
     * @param  Int    outletid 门店id
     * @param  String password 会员卡密码
     * @param  String vipno    会员卡号
     * @param  String mobile   手机号码
     * @param  String email    邮箱
	 * @return array
	 */
	public function addCard($map){
		
		$detail = D("Vipcard")->getDetail($map['cardid']);
		
		$map['use_jifen']   = $detail['init_jifen'];
		$map['total_jifen'] = $detail['init_jifen'];
		$map['use_money']   = $detail['init_money'];
		$map['total_money'] = $detail['init_money'];
		$map['cardname']    = $detail['title'];
		
		return $this->add($map);
	}
	
	/**
	 * 重置会员卡密码
	 * @access public
	 * @param  Int company  商户id
	 * @param  Int outletid 门店id
	 * @param  Int customid 会员信息id
	 * @param  String vipno 会员编号
	 * @param  String password 会员卡密码
	 * @return array
	 */
	public function reset($map){
		
		$password = myencrypt($map['password']);
		$newpwd   = myencrypt($map['newpassword']);
		$where    = "vipno='".$map['vipno']."' and customid=".$map['customid'];
		
		$data = $this->where($where)->find();
		
		if($data['password']==$password){
			
			if(empty($newpwd)){
				$newpwd = myencrypt(rand(100000,999999));
			}
			
			$this->where($where)->setField("password",$newpwd);
			
			wuserlog();//记录会员核心操作
			return array("info"=>"密码重置成功","status"=>1);
		}else{
			return array("info"=>"原密码不正确，请重试","status"=>-1);
		}
	}
	
	/**
	 * 更换会员卡门店
	 * @access public
	 * @param int $company  商户id
	 * @param int $newid    门店id
	 * @param int $customid 会员信息id
	 * @param String $vipno 会员卡号
	 * @return array
	 */
	public function changeOutlet($customid,$newid,$vipno,$company){
	
		$map['customid'] = $customid;
		$map['company']  = $company;
		$map['vipno']    = $vipno;
	
		$arr['outletid'] = $newid;
		
		wuserlog();//记录会员核心操作
		return $this->where($map)->setField("outletid",$newid);
	}
	
	/**
	 * 储值转账
	 * @access public
	 * @param  Int    company     商户id
	 * @param  Int    outletid    门店id
	 * @param  Int    customid    会员信息id
	 * @param  double money       转账金额
	 * @param  String vipno       转出会员卡号
	 * @param  String Input_vipno 转入会员卡号
	 * @param  String password    会员卡密码
	 * @return array
	 */
	public function transferMoney($map){
		
		$money = $map['money'];
			
		$arr['company']  = $map['company'];
		$arr['outletid'] = $map['outletid'];
		$arr['customid'] = $map['customid'];
		$arr['vipno']    = $map['vipno'];
		
		$cat['company']  = $map['company'];
		$cat['outletid'] = $map['outletid'];
		$cat['vipno']    = $map['input_vipno'];
		
		$password = myencrypt($map['password']);
		$view     = $this->where($arr)->find();
		$data     = $this->where($cat)->find();
		
		if($view['id']&&$data['id']){
			if($view['password']==$password){
				if($view['use_money']>$map['money']){
					
					$query = $this->where($arr)->setDec("use_money",$map['money']);
					$query = $this->where($cat)->setInc("use_money",$map['money']);
					
					//添加金额转账记录
					if($query){
						$view['remark'] = $map['remark']?$map['remark']:"储值转账，金额：".$map['money'];
						$this->addMoney($view,2,$money);
						
						wuserlog("转账金额：".$map['money'].",转入卡号：".$map['input_vipno']);//记录会员核心操作
						return array('info'=>'储值转账成功','status'=>1);
					}
				}else{
					return array('info'=>'储值转账失败，金额不足','status'=>-1);
				}
			}else{
				return array('info'=>'密码不正确，储值转账失败','status'=>-1);
			}
		}else{
			return array('info'=>'储值转账失败，请稍后重试','status'=>-1);
		}
	}
	
	/**
	 * 积分转账
	 * @access public
	 * @param  Int    company     商户id
	 * @param  Int    outletid    门店id
	 * @param  Int    customid    会员信息id
	 * @param  double credit      转账积分
	 * @param  String vipno       转出会员卡号
	 * @param  String Input_vipno 转入会员卡号
	 * @param  String password    会员卡密码
	 * @return array
	 */
	public function transferCredit($map){
	
		$credit = $map['credit'];
			
		$arr['company']  = $map['company'];
		$arr['outletid'] = $map['outletid'];
		$arr['customid'] = $map['customid'];
		$arr['vipno']    = $map['vipno'];
		
		$cat['company']  = $map['company'];
		$cat['outletid'] = $map['outletid'];
		$cat['vipno']    = $map['input_vipno'];
		
		$password = myencrypt($map['password']);
		$view     = $this->where($arr)->find();
		$data     = $this->where($cat)->find();
		
		if($view['id']&&$data['id']){
			if($view['password']==$password){
				if($view['use_jifen']>$map['credit']){
					
					$query = $this->where($arr)->setDec("use_jifen",$map['credit']);
					$query = $this->where($cat)->setInc("use_jifen",$map['credit']);
					
					//添加金额转账记录
					if($query){
						$view['remark']=$map['remark']?$map['remark']:"积分转账，积分：".$map['credit'];
						$this->addMoney($view,2,$credit);
						
						wuserlog("转账积分：".$map['credit']."，转入卡号：".$map['input_vipno']);//记录会员核心操作
						return array('info'=>'积分转账成功','status'=>1);
					}
				}else{
					return array('info'=>'积分转账失败，积分不足','status'=>-1);
				}
			}else{
				return array('info'=>'密码不正确，积分转账失败','status'=>-1);
			}
		}else{
			return array('info'=>'积分转账失败，请稍后重试','status'=>-1);
		}
	
	}
	
	/**
	 * 会员卡挂失(冻结)
	 * @access public
	 * @param  Int    company  商户id
	 * @param  Int    outletid 门店id
	 * @param  String vipno    会员卡号
	 * @return array
	 */
	public function frost($map){
		
		$view = $this->where($map)->find();
		
		if($view['id']&&$view['isdel']!=2){
			
			wuserlog();//记录会员核心操作
			return $this->where($map)->setField("isdel","2");
			
		}else{
			return;
		}
			
	}
	
	/**
	 * 会员卡挂失(解冻)
	 * @access public
	 * @param  Int    company  商户id
	 * @param  Int    outletid 门店id
	 * @param  String vipno    会员卡号
	 * @return array
	 */
	public function thaw($map){
		
		$view = $this->where($map)->find();
		
		if($view['id']&&$view['isdel']==2){
			
			wuserlog();//记录会员核心操作
			return $this->where($map)->setField("isdel","1");
				
		}else{
			return;
		}
			
	}
	
	/**
	 * 会员卡换卡
	 * @access public
	 * @param  Int    company  商户id
	 * @param  Int    outletid 门店id
	 * @param  String vipno     原会员卡号
	 * @param  String new_vipno 新的会员卡号
	 * @return array
	 */
	public function changeCard($map,$new_vipno){
		
		$data = $this->where($map)->setField("vipno",$new_vipno);
		
		wuserlog("新的卡号：".$new_vipno);//记录会员核心操作
		
		return $data;
	}
	
	/**
	 * 添加积分操作记录
	 * @access public
	 * @param  Int    company  商户id
	 * @param  Int    outletid 门店id
	 * @param  Int    vipid    会员卡关联id
	 * @param  Int    customid 会员信息id
	 * @param  Int    $val     积分
	 * @param  Int    $act     操作类型[1获得|2消费]
	 */
	public function addCredit($map,$act,$val){
		
		$arr['company']  = $map['company'];
		$arr['outletid'] = $map['outletid'];
		$arr['remark']   = $map['remark'];
		$arr['vipid']    = $map['id'];
		$arr['customid'] = $map['customid'];
		$arr['addtime']  = time();
		$arr['val']      = $val;
		$arr['act']      = $act;
		
		M("VipcardCredit")->add($arr);
	}
	
	/**
	 * 添加金额操作记录
	 * @access public
	 * @param  Int    company  商户id
	 * @param  Int    outletid 门店id
	 * @param  Int    vipid    会员卡关联id
	 * @param  Int    customid 会员信息id
	 * @param  Int    $val     金额
	 * @param  Int    $act     操作类型[1获得|2消费]
	 */
	public function addMoney($map,$act,$val){
		$arr['company']  = $map['company'];
		$arr['outletid'] = $map['outletid'];
		$arr['customid'] = $map['customid'];
		$arr['remark']   = $map['remark'];
		$arr['vipid']    = $map['id'];
		$arr['total']    = $val;
		$arr['addtime']  = time();
		$arr['val']      = $val;
		$arr['act']      = $act;
		
		M("VipcardMoney")->add($arr);
	}
	
	/**
	 * 验证会员卡号唯一性
	 * @access public
	 * @param  String vipno 会员卡号
	 * @return array
	 */
	public function checked($vipno){
	
		$map['vipno'] = $vipno;
			
		return $this->where($map)->find();
	}
	
	/**
     * 储值充值
     * @access public
     * @param  Int    company   商户id
     * @param  Int    outletid  门店id
     * @param  Int    customid  会员信息id
     * @param  String vipno     会员卡号
     * @param  double money     充值金额
     * @param  String remark    备注信息
     * @return array
     */
	public function rechargeMoney($map,$arr){
		
		$val  = $arr['use_money'];
		$view = $this->where($map)->find();
		
		if($view['id']){
			
			$data = $this->where($map)->setInc("use_money",$val);
			$data = $this->where($map)->setInc("total_money",$val);

			//添加金额操作记录
			if($data){
				$view['remark'] = $arr['remark']?$arr['remark']:"储值充值，金额：".$val;
				$this->addMoney($view, 1, $val);
				
				wuserlog("充值金额：".$val);//记录会员核心操作
				return array('info'=>'充值成功','status'=>1);
			}
		}else{
			return array('info'=>'充值失败，请稍后重试','status'=>-1);
		}
	}
	
	/**
	 * 储值消费
	 * @access public
	 * @param  Int    company   商户id
	 * @param  Int    outletid  门店id
	 * @param  Int    customid  会员信息id
	 * @param  String vipno     会员卡号
	 * @param  double money     充值金额
	 * @param  String remark    备注信息
	 * @return array
	 */
	public function consumptionMoney($map,$arr){
	
		$val      = $arr['use_money'];
		$password = myencrypt($arr['password']);
		$view     = $this->where($map)->find();
	
		if($view['id']){
			if($view['password']==$password){
				if($view['use_money']>=$val){
					
					$data = $this->where($map)->setDec("use_money",$val);
					//添加金额操作记录
					if($data){
						$view['remark'] = $arr['remark']?$arr['remark']:"储值消费，金额：".$val;
						$this->addMoney($view, 2, $val);
						
						wuserlog("消费金额：".$val);//记录会员核心操作
						return array('info'=>'储值消费成功','status'=>1);
					}
				}else{
					return array('info'=>'储值消费失败，金额不足','status'=>-1);
				}
			}else{
				return array('info'=>'密码不正确，请稍后重试','status'=>-1);
			}
		}else{
			return array('info'=>'储值消费失败，请稍后重试','status'=>-1);
		}
	}
	
	/**
	 * 积分充值
	 * @access public
	 * @param  Int    company   商户id
	 * @param  Int    outletid  门店id
	 * @param  Int    customid  会员信息id
	 * @param  String vipno     会员卡号
	 * @param  double credit    充值积分
	 * @param  String remark    备注信息
	 * @return array
	 */
	public function rechargeCredit($map,$arr){
	
		$val  = $arr['use_jifen'];
		$view = $this->where($map)->find();
	
		if($view['id']){
				
			$data = $this->where($map)->setInc("use_jifen",$val);
			$data = $this->where($map)->setInc("total_jifen",$val);
	
			//添加金额操作记录
			if($data){
				$view['remark'] = $arr['remark']?$arr['remark']:"积分充值，积分：".$val;
				$this->addCredit($view, 1, $val);
				
				wuserlog("充值积分：".$val);//记录会员核心操作
				return array('info'=>'积分充值成功','status'=>1);
			}
		}else{
			return array('info'=>'积分充值失败，请稍后重试','status'=>-1);
		}
	}
	
	/**
	 * 积分消费
	 * @access public
	 * @param  Int    company   商户id
	 * @param  Int    outletid  门店id
	 * @param  Int    customid  会员信息id
	 * @param  String vipno     会员卡号
	 * @param  double credit    消费积分
	 * @param  String remark    备注信息
	 * @return array
	 */
	public function consumptionCredit($map,$arr){
	
		$val      = $arr['use_jifen'];
		$password = myencrypt($arr['password']);
		$view     = $this->where($map)->find();
	
		if($view['id']){
			if($view['password']==$password){
				if($view['use_jifen']>=$val){
					
					$data = $this->where($map)->setDec("use_jifen",$val);
					
					//添加金额操作记录
					if($data){
						$view['remark'] = $arr['remark']?$arr['remark']:"积分消费，积分：".$val;
						$this->addCredit($view, 2, $val);
						
						wuserlog("消费积分：".$val);//记录会员核心操作
						return array('info'=>'积分消费成功','status'=>1);
					}
				}else{
					return array('info'=>'积分消费失败，积分不足','status'=>-1);
				}
			}else{
				return array('info'=>'密码不正确，请稍后重试','status'=>-1);
			}
		}else{
			return array('info'=>'积分消费失败，请稍后重试','status'=>-1);
		}
	}
	
	/**
	 * 批量赠送积分
	 * @access public
	 * @param  Int    company   商户id
	 * @param  Int    outletid  门店id
	 * @param  Int    credit    要赠送的积分
	 * @param  String remark    备注信息
	 * @param  String val       选中的会员信息id
	 * @return array
	 */
	public function getIntegral($map){

		$arr['company']  = $map['company'];
		$arr['outletid'] = $map['outletid'];
		
		if($map['val']){//给指定会员赠送积分
			$idArr = explode(',',$map['val']);
			
			foreach($idArr as $k=>$v){
				
				$arr['customid'] = $v;
				$view = $this->where($arr)->find();
				
				if($view['id']){
					$query = $this->where("id=".$view['id'])->setInc('use_jifen',$map['credit']);
					$query = $this->where("id=".$view['id'])->setInc('total_jifen',$map['credit']);
					
					//添加积分操作记录
					if($query){
						$view['remark'] = $map['remark']?$map['remark']:"获得赠送积分：".$map['credit']."分";
						$this->addCredit($view, 1, $map['credit']);
						
						wuserlog("获得赠送积分：".$map['credit']);//记录会员核心操作
					}
				}
			}
		}else{//给所有会员赠送积分
			$arr['isdel'] = 1;
			$data = $this->where($arr)->select();
			
			foreach($data as $k=>$v){
				
				$query = $this->where("id=".$v['id'])->setInc('use_jifen',$map['credit']);
				$query = $this->where("id=".$v['id'])->setInc('total_jifen',$map['credit']);
				
				//添加积分操作记录
				if($query){
					$data[$k]['remark'] = $map['remark']?$map['remark']:"获得赠送积分：".$map['credit']."分";
					$this->addCredit($data[$k], 1, $map['credit']);
					
					wuserlog("获得赠送积分：".$map['credit']);//记录会员核心操作
				}
			}
		}
		return array("info"=>'积分赠送成功','status'=>1);
	}
	
	/**
	 * 批量赠送储值
	 * @access public
	 * @param  Int    company   商户id
	 * @param  Int    outletid  门店id
	 * @param  Int    money     要赠送的金额
	 * @param  String remark    备注信息
	 * @param  String val       选中的会员信息id
	 * @return array
	 */
	public function getMoney($map){
	
		$arr['company']  = $map['company'];
		$arr['outletid'] = $map['outletid'];
	
		if($map['val']){//给指定会员赠送积分
			$idArr = explode(',',$map['val']);
				
			foreach($idArr as $k=>$v){
	
				$arr['customid'] = $v;
				$view = $this->where($arr)->find();
	
				if($view['id']){
					$query = $this->where("id=".$view['id'])->setInc('use_money',$map['money']);
					$query = $this->where("id=".$view['id'])->setInc('total_money',$map['money']);
						
					//添加金额操作记录
					if($query){
						$view['remark'] = $map['remark']?$map['remark']:"获得赠送储值：".$map['money']."元";
						$this->addMoney($view, 1, $map['money']);
						
						wuserlog("获得赠送储值：".$map['money']);//记录会员核心操作
					}
				}
			}
		}else{//给所有会员赠送储值
			$arr['isdel'] = 1;
			$data = $this->where($arr)->select();
				
			foreach($data as $k=>$v){
	
				$query = $this->where("id=".$v['id'])->setInc('use_money',$map['money']);
				$query = $this->where("id=".$v['id'])->setInc('total_money',$map['money']);
	
				//添加积分操作记录
				if($query){
					$data[$k]['remark'] = $map['remark']?$map['remark']:"获得赠送储值：".$map['money']."元";
					$this->addMoney($data[$k], 1, $map['money']);
					
					wuserlog("获得赠送储值：".$map['money']);//记录会员核心操作
				}
			}
		}
		return array("info"=>'储值赠送成功','status'=>1);
	}
	
	/**
     * 批量积分清零
     * @access public
     * @param  String val 选中的会员信息id
     * @return array
     */
	public function clearIntegral($map){
		
		$arr['company']  = $map['company'];
		$arr['outletid'] = $map['outletid'];
		
		if($map['val']){
			
			$idArr = explode(',',$map['val']);
			foreach($idArr as $k=>$v){
				
				$arr['customid'] = $v;
				$view = $this->where($arr)->find();
				
				$query = $this->where("id=".$view['id'])->setField('use_jifen',0);
				
				//添加积分操作记录
				if($query){
					$view['remark'] = $map['remark']?$map['remark']:"积分清零";
					$this->addCredit($view, 2, $view['use_jifen']);
					
					wuserlog();//记录会员核心操作
				}
			}
		}else{
			$arr['isdel'] = 1;
			$data = $this->where($arr)->select();
				
			foreach($data as $k=>$v){
			
				$query = $this->where("id=".$v['id'])->setField('use_jifen',0);
			
				//添加积分操作记录
				if($query){
					$data[$k]['remark'] = $map['remark']?$map['remark']:"积分清零";
					$this->addCredit($data[$k], 1, $v['use_jifen']);
					
					wuserlog();//记录会员核心操作
				}
			}
		}
		return array('info'=>'积分清除成功','status'=>1);
	}
	
	/**
	 * 批量储值清零
	 * @access public
	 * @param  String val 选中的会员信息id
	 * @return array
	 */
	public function clearMoney($map){
	
		$arr['company']  = $map['company'];
		$arr['outletid'] = $map['outletid'];
	
		if($map['val']){
				
			$idArr = explode(',',$map['val']);
			foreach($idArr as $k=>$v){
	
				$arr['customid'] = $v;
				$view = $this->where($arr)->find();
	
				$query = $this->where("id=".$view['id'])->setField('use_money',0);
				
				//添加金额操作记录
				if($query){
					$view['remark'] = $map['remark']?$map['remark']:"储值清零";
					$this->addMoney($view, 2, $view['use_money']);
					
					wuserlog();//记录会员核心操作
				}
			}
		}else{
			$arr['isdel'] = 1;
			$data = $this->where($arr)->select();
	
			foreach($data as $k=>$v){
				
				$query = $this->where("id=".$v['id'])->setField('use_money',0);
				//添加金额操作记录
				if($query){
					$data[$k]['remark'] = $map['remark']?$map['remark']:"储值清零";
					$this->addMoney($data[$k], 1, $v['use_money']);
					
					wuserlog();//记录会员核心操作
				}
			}
		}
		return array('info'=>'储值清除成功','status'=>1);
	}
	
	
	
}